package com.srcbox.file.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSONArray
import com.srcbox.file.R
import com.srcbox.file.ui.util.ShitSmooth
import com.srcbox.file.ui.util.WallPaper
import com.srcbox.file.util.EggUtil

class FunList(
    private val context: Context,
    private val appFunJson: JSONArray
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.fun_list, parent, false)
        return MeHolder(view)
    }

    override fun getItemCount(): Int {
        return appFunJson.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MeHolder) {
            val jsonA = appFunJson.getJSONArray(position)
            val name = jsonA[0].toString()
            val icon = jsonA[1].toString()
            holder.bind(name, icon)
        }
    }

    inner class MeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bind(name: String, icon: String) {
            itemView.findViewById<TextView>(R.id.fun_name).text = name
            EggUtil.setViewIcon<ImageView>(
                context,
                itemView.findViewById(R.id.fun_icon),
                icon
            )
            when (name) {
                "壁纸" -> {
                    itemView.setOnClickListener {
                        context.startActivity(Intent(context, WallPaper::class.java))
                    }
                }
                "狗屁不通" -> {
                    itemView.setOnClickListener {
                        context.startActivity(Intent(context, ShitSmooth::class.java))
                    }
                }
                else -> {
                    itemView.findViewById<TextView>(R.id.fun_name)
                        .setTextColor(Color.parseColor("#cccccc"))
                    itemView.setOnClickListener {
                        EggUtil.toast(context, "敬请期待")
                    }
                }
            }

            /*
            itemView.setOnClickListener {
                when (name) {
                    "壁纸" -> {
                    }
                    "狗屁不通" -> {

                    }
                    else -> {
                        EggUtil.toast(context, "敬请期待")
                    }
                }
            }*/
        }
    }
}